package day17.tarena.GUI;

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.io.*;
	/**
	 * 
	 * 记事本 Version_1.01
	 * @author WangPeihu
	 *
	 */
public class MyNotepad implements ActionListener {
	JTextArea jt = new JTextArea(100000,100000);
	JFrame jf = new JFrame("记事本 Version 1.0");
	JPanel guanyu = new JPanel();
	JScrollPane jp = new JScrollPane(jt);

	JFileChooser jfc = new JFileChooser();
	JFileChooser save = new JFileChooser();
	String openaddress,saveaddress ;
	String text;
	
	public void actionPerformed(ActionEvent e) {
		String n = e.getActionCommand();
		if ("新建".equals(n)) {
			if(jt.getText().length() >0){
				int choice = JOptionPane.showConfirmDialog(null,
				"文件另存为");
				if (choice == JOptionPane.YES_OPTION) {
					try {
						save();
					} catch (Exception e1) {
						e1.printStackTrace();
					}
				}	
			}
		} else if ("打开".equals(n)) {
				try {
					open();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			jt.setText(text);
		} else if ("保存".equals(n)) {
				try {
					save();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
		} else if ("另存".equals(n)) {
			try {
				save();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		} else if ("退出".equals(n)) {
			int choice = JOptionPane.showConfirmDialog(null,
					"你真的要退出吗？");
			if (choice == JOptionPane.YES_OPTION) {
				System.exit(0);
			}	
		} else if ("全选".equals(n)) {
				jt.selectAll();
		} else if ("剪切".equals(n)) {
				jt.cut();
		} else if ("复制".equals(n)) {
				jt.copy();
		} else if ("粘贴".equals(n)) {
				jt.paste();
		}else if ("颜色".equals(n)) {
			Color c=JColorChooser.showDialog(jf, "请选择文字颜色", Color.BLACK);
			jt.setForeground(c);
		}else if ("帮助".equals(n)) {
			JOptionPane.showConfirmDialog(null,  "你想怎么做就怎么做。" ,
						   "帮助", JOptionPane.CLOSED_OPTION);
		}else if ("版本".equals(n)) {
			 JOptionPane.showConfirmDialog(null,  "Version_1.01  Wangpeihu" ,
					   "Version", JOptionPane.CLOSED_OPTION);
		}else if ("关于".equals(n)) {
			 JOptionPane.showConfirmDialog(null,  "关于这个记事本，好多功能没有完善" +
				   		"	，请等待开发。","about", JOptionPane.CLOSED_OPTION);
		}
	}

	/**
	 * 
	 * @打开方法
	 */
	public void open() throws Exception {
		Component parent = null;
		int returnVal = jfc.showOpenDialog(parent);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			openaddress = (jfc.getCurrentDirectory() + "/" + jfc
					.getSelectedFile().getName());
		}
		FileInputStream fis = new FileInputStream(openaddress);
		byte[] b = new byte[10240];
			while (true) {
				int temp = fis.read(b);
				if (temp == -1) {
					break;
				}
				text = text + new String(b, 0, temp);
			 	text =  new String (
		 				text.getBytes("GBK"),"UTF-8");
			}
		fis.close();
	}
	
	/**
	 * 
	 * @保存方法
	 * 
	 */
	public void save() throws Exception{
		Component parent = null;
		int comm = jfc.showSaveDialog(parent);
		if (comm == JFileChooser.APPROVE_OPTION) {
			saveaddress = (jfc.getCurrentDirectory() + "/" +
					jfc	.getSelectedFile().getName());
		}
		FileOutputStream fos = new
			FileOutputStream(saveaddress);
		fos.write((jt.getText()).getBytes());
		fos.close();
	}
			
	public MyNotepad() {
		JMenuBar jmb = new JMenuBar();
		String[] jmLab = { "文件", "编辑", "帮助" };
		String[][] jmiLab = { { "新建", "打开", "保存", "另存", "", "退出" },
				{ "全选", "剪切", "复制", "粘贴", "颜色",}, { "帮助", "版本", "关于" } };
		for (int i = 0; i < jmLab.length; i++) {
			JMenu jm = new JMenu(jmLab[i]);
			jmb.add(jm);
			for (int j = 0; j < jmiLab[i].length; j++) {
				if ("".equals(jmiLab[i][j])) {
					jm.addSeparator();
				} else {
					JMenuItem jmi = new JMenuItem(jmiLab[i][j]);
					jm.add(jmi);
					jmi.addActionListener(this);
				}
			}
		}
		// 常用方法有对两个方法介绍

		jf.setJMenuBar(jmb);
		jf.add(jt, BorderLayout.NORTH);
		jf.setLocation(400, 400);
		jf.setSize(600, 600);
		jf.setVisible(true);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public static void main(String[] args) {
		new MyNotepad();
	}
	
}